<?php

namespace App\Module\Api\Controllers;

use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\FeedbackRequest;
use App\Module\Api\Validator\FeedbackValidator;
use App\System\Activity\Repositories\ActiveInfoRepository;
use App\System\Cms\Repositories\CmsArticleRepository;
use App\System\Cms\Repositories\CmsFeedbackRepository;
use App\System\Coupon\Repositories\CourClassRepository;
use Illuminate\Http\Request;
use Symfony\Component\VarDumper\Dumper\DataDumperInterface;


class FeedbackSubmitController extends Controller
{
    public function __construct()
    {
        $this->query=new FeedbackRequest();
        parent::__construct();
    }

    public function index(){
        (new FeedbackValidator())->check();
        $action = $this->query->action;
        $cache = \Cache::get(\request()->s);
        $user_id = $cache['userInfo']->id;
        if ($action == 1){
            //一般反馈
            $feedBack = CmsFeedbackRepository::getInstance();

            $flag = $feedBack->createdNormal($this->query,$user_id);
            if (!$flag){
                throw new ParamsException([
                    "error_code"=>"1000",
                    "message"=>"提交失败"
                ]);
            }
        }else{
//            根据不一样的action 验证id

            switch ($action){
                case  2:
                    $article=CmsArticleRepository::getInstance();
                    $title = $article->getArticleTitleById($this->query->id,'title');
                    break;
                case 3 :
                    $article=CourClassRepository::getInstance();
                    $title = $article->getCourClassNameById($this->query->id,'name');
                    break;
                case 4 :
                    $article=ActiveInfoRepository::getInstance();
                    $title = $article->getActiveTitleById($this->query->id,'name');
                    break;
            }
            if (!$title){
                throw new ParamsException([
                    'error_code'=>'1000',
                    "message"=>'非法操作'
                ]);
            }
            $data=[
                'mobile'=>$this->query->mobile,
                'type'=>$this->query->action,
                'title'=>$title,
                'foreign_key_id'=>$this->query->id,
                'content'=>$this->query->content,
                'user_id'=>$user_id,
            ];
               $feedBack = CmsFeedbackRepository::getInstance();
               $flag=$feedBack->createOtherFedd($data);
               if (!$flag){
                   throw new ParamsException([
                       'error_code'=>'1000',
                       "message"=>'提交失败'
                   ]);
               }
               return apiResponse(['d'=>'提交成功']);
        }
    }
}
